#!/usr/bin/python
#-*- coding: UTF-8 -*-
from __future__ import unicode_literals

from flask import (Flask, render_template, redirect, url_for, request, flash)
from flask_bootstrap import Bootstrap
from flask_login import login_required, login_user, logout_user, current_user

from forms import TodoListForm, LoginForm,TextForm
from ext import db, login_manager
from models import TodoList, User
import time

SECRET_KEY = 'This is my key'

app = Flask(__name__)
bootstrap = Bootstrap(app)

app.secret_key = SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://xiang:xiang2018@127.0.0.1/test"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True


db.init_app(app)
login_manager.init_app(app)
login_manager.login_view = "login"


@app.route('/', methods=['GET', 'POST'])
@login_required
def show_todo_list():
    form = TodoListForm()
    if request.method == 'GET':
        # user_id,equipmnet, xinghao,bumen,owner,dispaly,cpu,memory,disk,mac_addr,guzhang,create_time
        todolists = TodoList.query.all()
        return render_template('index.html', todolists=todolists, form=form,create_time=time.strftime("%Y-%m-%d %X", time.localtime()))
    else:
        if form.validate_on_submit():
            """
           todolist = TodoList(current_user.id, form.equipmnet.data, form.xinghao.data, form.bumen.data,
                                form.owner.data, form.dispaly.data, form.cpu.data, form.memory.data, form.disk.data,
                                form.mac_addr.data, form.guzhang.data, form.create_time.data)
           """
            todolist = TodoList(current_user.id, form.equipmnet.data, form.xinghao.data, form.bumen.data,
                                form.owner.data, form.dispaly.data, form.cpu.data, form.memory.data, form.disk.data,
                                form.mac_addr.data,form.guzhang.data,form.create_time.data)

            db.session.add(todolist)
            db.session.commit()
            flash('添加成功')
        else:
            flash(form.errors)
        return redirect(url_for('show_todo_list'))


@app.route('/delete/<int:id>')
@login_required
def delete_todo_list(id):
     todolist = TodoList.query.filter_by(id=id).first_or_404()
     db.session.delete(todolist)
     db.session.commit()
     flash('You have delete a todo list')
     return redirect(url_for('show_todo_list'))


@app.route('/change/<int:id>', methods=['GET', 'POST'])
@login_required
def change_todo_list(id):
    if request.method == 'GET':
        form = TodoListForm()
        todolist = TodoList.query.filter_by(id=id).first_or_404()
        form.equipmnet.data = todolist.equipmnet
        form.xinghao.data = todolist.xinghao
        form.bumen.data = todolist.bumen
        form.owner.data = todolist.owner
        form.dispaly.data = todolist.dispaly
        form.cpu.data = todolist.cpu
        form.memory.data = todolist.memory
        form.disk.data = todolist.disk
        form.mac_addr.data = todolist.mac_addr
        form.guzhang.data = todolist.guzhang
        form.create_time.data = todolist.create_time
        #form.status.data = str(todolist.status)
        return render_template('modify.html', form=form)
    else:
        form = TodoListForm()
        if form.validate_on_submit():
            todolist = TodoList.query.filter_by(id=id).first_or_404()
            todolist.equipmnet = form.equipmnet.data
            todolist.xinghao = form.xinghao.data
            todolist.bumen = form.bumen.data
            todolist.owner = form.owner.data
            todolist.dispaly = form.dispaly.data
            todolist.cpu = form.cpu.data
            todolist.memory = form.memory.data
            todolist.disk = form.disk.data
            todolist.mac_addr = form.mac_addr.data
            todolist.guzhang = form.guzhang.data
            todolist.create_time = form.create_time.data
            db.session.commit()
            flash('修改成功')
        else:
            flash(form.errors)
        return redirect(url_for('show_todo_list'))


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        user = User.query.filter_by(username=request.form['username'], password=request.form['password']).first()
        if user:
            login_user(user)
            flash('you have logged in!')
            return redirect(url_for('show_todo_list'))
        else:
            flash('Invalid username or password')
    form = LoginForm()
    return render_template('login.html', form=form)

@app.route('/kk', methods=['GET', 'POST'])
@login_required
def hello():
    form = TextForm()
    return render_template('kk.html', form=form)


@app.route('/logout')
@login_required
def logout():
    logout_user()
    flash('退出登录')
    return redirect(url_for('login'))


@login_manager.user_loader
def load_user(user_id):
    return User.query.filter_by(id=int(user_id)).first()


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
